/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.oschina.durcframework.easymybatis;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.oschina.durcframework.easymybatis.handler.FillHandler;

/**
 * 配置
 * 
 * @author tanghc
 */
public class EasymybatisConfig {
	
	private static final String COMMON_SQL_CLASSPATH = "easymybatis/commonSql.xml";

	// 驼峰转换下划线
	private boolean camel2underline = Boolean.TRUE;
	// 生成mapper的线程池大小
	private int mapperExecutorPoolSize = 20;
	// 模板文件classpath
	private String templateClasspath;
	// 通用mapper路径
	private String commonSqlClasspath = COMMON_SQL_CLASSPATH;
	// mapper文件保存文件夹地址，如：C:/mapper
	private String mapperSaveDir;
	
	private Map<String, FillHandler<?>> fillMap = new HashMap<>(8);
	
	/**
	 * 驼峰转换下划线，如果配置了true，则java字段映射成数据库字段将自动转成下划线形式
	 * 
	 * @param camel2underline
	 */
	public void setCamel2underline(boolean camel2underline) {
		this.camel2underline = camel2underline;
	}

	/**
	 * 是否执行驼峰转下划线. 如果配置了true，则java字段映射成数据库字段将自动转成下划线形式
	 * 
	 * @return
	 */
	public boolean isCamel2underline() {
		return camel2underline;
	}

	public int getMapperExecutorPoolSize() {
		return mapperExecutorPoolSize;
	}

	public void setMapperExecutorPoolSize(int mapperExecutorPoolSize) {
		this.mapperExecutorPoolSize = mapperExecutorPoolSize;
	}

	public String getTemplateClasspath() {
		return templateClasspath;
	}

	public void setTemplateClasspath(String templateClasspath) {
		this.templateClasspath = templateClasspath;
	}

	public String getCommonSqlClasspath() {
		return commonSqlClasspath;
	}

	public void setCommonSqlClasspath(String commonSqlClasspath) {
		this.commonSqlClasspath = commonSqlClasspath;
	}

	public String getMapperSaveDir() {
		return mapperSaveDir;
	}

	/**
	 * 将mapper文件保存到指定文件夹中<br>
	 * 因为easymybatis是直接将mapper内容注入到内存当中，开发人员无感知，并且不知道mapper内容是什么样子<br>
	 * 这个功能就是让开发人员能够查看到对应的mapper内容，方便定位和排查问题。<br>
	 * 一般情况下此项不用开启。
	 * @param mapperSaveDir mapper文件保存文件夹地址，如：C:/mapper
	 */
	public void setMapperSaveDir(String mapperSaveDir) {
		File dir = new File(mapperSaveDir);
		boolean selfMake = false;
		if(!dir.exists()) {
			dir.mkdirs();
			selfMake = true;
		}
		if(!dir.isDirectory()) {
			if(selfMake) {
				dir.delete();
			}
			throw new IllegalArgumentException("mapperSaveDir必须是一个文件夹路径，，mapperSaveDir：" + mapperSaveDir);
		}
		this.mapperSaveDir = mapperSaveDir;
	}
	
	/**
	 * 设置字段填充
	 * @param fills
	 */
	public <T extends FillHandler<?>> void setFills(List<T> fills) {
		for (FillHandler<?> fill : fills) {
			this.addFill(fill);
		}
	}
	
	public void addFill(FillHandler<?> fill) {
		fillMap.put(fill.getColumnName(), fill);
	}
	
	public FillHandler<?> getFill(String columnName) {
		return fillMap.get(columnName);
	}
}
